package cn.xxm.strategy.pay.i18n.request;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
@ServletComponentScan
//@WebFilter(urlPatterns = "/*",filterName = "i18nChannelFilter1")
@Order(1)
@Slf4j
public class I18nChannelFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info(">>>config:{}", JSONObject.toJSONString(filterConfig));
        log.info("i18n ChannelFilter init>>>>>>>>>");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        ServletRequest requestWrapper = null;

        if(servletRequest instanceof HttpServletRequest) {
        	String contentType = servletRequest.getContentType();
			log.info("i18n,contentType:{}",contentType);
        	if(StringUtils.isBlank(contentType) || !contentType.contains("multipart/form-data")) {
        		requestWrapper = new RequestWrapper((HttpServletRequest) servletRequest);
        	}

        }
        if(requestWrapper == null) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            filterChain.doFilter(requestWrapper, servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
